#include "camera.h"
#include <QDebug>

void Camera::initCamera(QLabel *parent=nullptr)
{
    //获取摄像头列表
    infolist=QCameraInfo::availableCameras();
    if(infolist.isEmpty())return;
    //默认使用第一个
    info=infolist[0];
    camera=new QCamera(info);
    imageCapture=new QCameraImageCapture(camera);
    viewfinder=new QCameraViewfinder(parent);
    viewfinder->resize(parent->size());
    //默认输出区域，空
    camera->setViewfinder(viewfinder);
    //开启摄像头
    connect(imageCapture,&QCameraImageCapture::imageCaptured,this,[=](int id,QImage image){
        qDebug()<<"capture success";
        emit captureSuccessed(image);
    });
}

void Camera::destroyCamera()
{

    delete camera;
    delete  viewfinder;
    delete  imageCapture;
}


QList<QCameraInfo> Camera::getCameraInfo()
{
    return infolist;
}

void Camera::setCamera(int index)
{
    camera->stop();
    delete  camera;
    camera=new QCamera(infolist[index]);
    camera->setViewfinder(viewfinder);
    camera->start();
    qDebug()<<infolist[index].description();

}

void Camera::setShowLabel(QLabel *label)
{
    viewfinder->setParent(label);
    viewfinder->resize(label->size());
}


void Camera::captureImg()
{
    imageCapture->capture();
}

QString Camera::getImgName()
{
    QDateTime datatime=QDateTime::currentDateTime();
    return datatime.toString("yyyy-mm-dd-hh:mm:ss.png");

}



void Camera::stopCamera()
{
    camera->stop();
    viewfinder->close();

}

void Camera::startCamera()
{
    camera->start();
    viewfinder->show();

}
